@ECHO OFF

rem =============================================================================
rem TESTED ON: curl-7.21.2
rem REQUIRES: cygwin: echo, sed, rm
rem           visual studio
rem DEPENDENCIES: openssl, zlib
rem PATH: echo, sed
rem HOWTO: docs\INSTALL
rem =============================================================================

rem =====SETTINGS================================================================

rem Library architecture, possible values: Win32, x64
set ARCH=Win32

rem Build debug or release version, possible values: Debug, Release
set BUILD_TYPE=Release

rem Library type, possible values: Static, Dynamic
set LIBRARY_TYPE=Dynamic

rem Visual C++ version, possible values: 8, 9, 10
set VC_VERSION=10

rem Visual Studio path
set VC_PATH=C:\Program Files (x86)\Microsoft Visual Studio 10.0

rem Directory where curl will be installed
set INSTALL_ROOT=%CD%

rem Zlib installed library path (use build-zlib.bat)
set ZLIB_INSTALL_PATH=%INSTALL_ROOT%\vs_%VC_VERSION%\%ARCH%\%BUILD_TYPE%\%LIBRARY_TYPE%\zlib

rem OpenSSL not installed libraries path (use build-openssl.bat)
set OPENSSL_INSTALL_PATH=%INSTALL_ROOT%\vs_%VC_VERSION%\%ARCH%\%BUILD_TYPE%\%LIBRARY_TYPE%\openssl

rem Curl sources path
set CURL_SRC_PATH=%CD%\src\curl-7.21.2

rem Temp directory
set TMP_DIR=c:\x\tmp

rem Path to return after compilation
set RETURN_PATH=%CD%

rem =============================================================================

set INSTALL_PATH=%INSTALL_ROOT%\vc_%VC_VERSION%\%ARCH%\%BUILD_TYPE%\%LIBRARY_TYPE%\curl


set ZLIB_PATH=%TMP_DIR%\curl\zlib
set OPENSSL_PATH=%TMP_DIR%\curl\openssl

rem This 2 lines clean previous build results, it may fail.
rmdir /S /Q "%ZLIB_PATH%"
rmdir /S /Q "%OPENSSL_PATH%"

echo d | xcopy "%ZLIB_INSTALL_PATH%\include\zlib\*" "%ZLIB_PATH%" /i /y
echo d | xcopy "%OPENSSL_INSTALL_PATH%\include\*" "%OPENSSL_PATH%\inc32" /i /y /s

if %LIBRARY_TYPE% == Static (
    echo f | xcopy "%ZLIB_INSTALL_PATH%\lib\*.lib" "%ZLIB_PATH%\zlib.lib" /y
    echo d | xcopy "%OPENSSL_INSTALL_PATH%\lib\*" "%OPENSSL_PATH%\out32" /i /y
) else (
    echo f | xcopy "%ZLIB_INSTALL_PATH%\lib\*.lib" "%ZLIB_PATH%\zdll.lib" /y
    echo f | xcopy "%ZLIB_INSTALL_PATH%\lib\*.exp" "%ZLIB_PATH%\zdll.exp" /y
    echo d | xcopy "%OPENSSL_INSTALL_PATH%\lib\*" "%OPENSSL_PATH%\out32dll" /i /y
    echo d | xcopy "%OPENSSL_INSTALL_PATH%\bin\*" "%OPENSSL_PATH%\out32dll" /i /y
)


rem http://msdn.microsoft.com/en-us/library/x4d2c09s(v=VS.80).aspx
if %ARCH% == Win32 (
    call "%VC_PATH%\VC\bin\vcvars32.bat"
) else (
    call "%VC_PATH%\VC\vcvarsall.bat" x86_amd64
)


cd "%CURL_SRC_PATH%"
nmake vc%VC_VERSION%

if %BUILD_TYPE% == Debug (
    set CFG_PREFIX=debug
) else (
    set CFG_PREFIX=release
)

if %LIBRARY_TYPE% == Static (
    set CFG=%CFG_PREFIX%-ssl-zlib
) else (
    set CFG=%CFG_PREFIX%-dll-ssl-dll-zlib-dll
)

set NMAKEPARAM=cfg=%CFG%

if %ARCH% == x64 (
   set NMAKEPARAM=MACHINE=x64 %NMAKEPARAM%
)

cd "%CURL_SRC_PATH%\lib"
rm -rfd release-*
rm -rfd debug-*
nmake /f Makefile.vc%VC_VERSION% %NMAKEPARAM%


echo d | xcopy "%CURL_SRC_PATH%\include\curl\*.h" "%INSTALL_PATH%\include\curl" /e /i /y /s

if %LIBRARY_TYPE% == Static (
    echo d | xcopy %CFG%\*.lib "%INSTALL_PATH%\lib" /i /y
) else (
    echo d | xcopy %CFG%\*.exp "%INSTALL_PATH%\lib" /i /y
    echo d | xcopy %CFG%\*.lib "%INSTALL_PATH%\lib" /i /y
    echo d | xcopy %CFG%\*.dll "%INSTALL_PATH%\bin" /i /y
)


rmdir "%TMP_DIR%\curl" /S /Q


cd "%RETURN_PATH%"

@ECHO ON
